package com.example.myapplication.data.database

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import android.content.Context

/**
 * 储物空间管理应用数据库
 */
@Database(
    entities = [StorageSpaceEntity::class, StorageItemEntity::class],
    version = 1,
    exportSchema = false
)
abstract class StorageDatabase : RoomDatabase() {
    
    abstract fun storageSpaceDao(): StorageSpaceDao
    abstract fun storageItemDao(): StorageItemDao
    
    companion object {
        @Volatile
        private var INSTANCE: StorageDatabase? = null
        
        fun getDatabase(context: Context): StorageDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    StorageDatabase::class.java,
                    "storage_database"
                )
                    .addCallback(StorageDatabaseCallback())
                    .build()
                INSTANCE = instance
                instance
            }
        }
        
        /**
         * 数据库创建回调
         */
        private class StorageDatabaseCallback : Callback() {
            override fun onCreate(db: SupportSQLiteDatabase) {
                super.onCreate(db)
                // 可以在这里添加初始数据
            }
        }
        
        /**
         * 数据库迁移策略（未来版本升级时使用）
         */
        val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 未来版本升级时的SQL语句
            }
        }
    }
} 